package Controller;

import Model.CSGrade;
import Model.CSGradeBean;
import Model.Sport;
import Model.SportBean;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/Dafen2")
public class Dafen2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Context context = null;
        HttpSession session = request.getSession();
        String sno = request.getParameter("sno");
        try {
            context = new InitialContext();
            Context contextNeeded = (Context) context.lookup("java:comp/env");
            DataSource ds = (DataSource) contextNeeded.lookup("wq");// 获得连接池。
            Connection conn = null;
            ResultSet rs;
            String sql;
            PreparedStatement pstmt;
            conn = ds.getConnection();//获得数据源
            sql = "select score.id,name,dept,class,grade from score natural join user where sno=?";
            pstmt =  conn.prepareStatement(sql);
            pstmt.setString(1,sno);
            rs = pstmt.executeQuery();
            CSGradeBean sb = (CSGradeBean) session.getAttribute("csgradeBean");
            if(sb==null){
                sb = new CSGradeBean();
            }
            CSGradeBean temp = new CSGradeBean();
            CSGrade d;
            while (rs.next()){
                d = new CSGrade();
                d.setSno(sno);
                d.setId(rs.getString(1));
                d.setName(rs.getString(2));
                d.setDept(rs.getString(3));
                d.setSclass(rs.getString(4));
                d.setGrade(rs.getString(5));
                temp.add(d);
            }
            sb.setCsGradeArrayList(temp.getCsGradeArrayList());
            session.setAttribute("csgradeBean",sb);
            conn.close();
            response.sendRedirect("caipan/dafen2.jsp");
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("加载不成功");
        }
    }
}
